toPositiveIntOrNull

@ExperimentalSinceKotoolsTypes(version = "4.3.1")
fun Number.toPositiveIntOrNull(): PositiveInt?

Returns this number as a PositiveInt, which may involve rounding or truncation, or returns null if this number is strictly negative.

var result: PositiveInt? = 1.toPositiveIntOrNull()
println(result) // 1

result = 0.toPositiveIntOrNull()
println(result) // 0

result = (-1).toPositiveIntOrNull()
println(result) // null

You can use the toPositiveIntOrThrow function for throwing an IllegalArgumentException instead of returning null when this number is strictly negative.